"""
通过Python + Flask实现mock平台搭建
了解一下接口实现的原理
"""
import json
from flask import Flask, jsonify, request

# 创建一个应用对象
app = Flask(__name__)


# 定义视图函数，设置路由规则
@app.route("/index")
def index():
    print("访问的index主页")
    return "hello mock"


# {"mobile": "13800000002", "password": "123456"}
@app.route("/api/sys/login", methods=["POST"])  # 上一个接口用的get请求，相当是可以直接省略，默认的get请求，post不能省略
def login():
    result = json.loads(request.get_data().decode("utf-8"))
    mobile = result.get("mobile")
    password = result.get("password")
    print(mobile, password)
    if mobile == "13800000002" and password == "123456":
        data = {
            "success": True,
            "code": 10000,
            "message": "操作成功！",
            "token": "90ef03e8-16ff-4d48-bca2-1539889d7191"
        }
    else:
        data = {
            "success": False,
            "code": 99999,
            "message": "抱歉，系统繁忙，请稍后重试！",
            "token": "90ef03e8-16ff-4d48-bca2-1539889d7191"
        }
    return data


if __name__ == '__main__':
    # 启动WEB服务器
    app.run()
